<?php
class FrbFlatClientController extends DooController
{

  public function __construct() {

  }

  public function getAll() {
    Doo::loadModelAt('FrbFlatChambre', 'frbFlat');
    Doo::loadModelAt('FrbFlatImmeuble', 'frbFlat');
    Doo::loadModelAt('FrbFlatVille', 'frbFlat');
    $appart = new FrbFlatChambre();
    $appart = $appart->relate('FrbFlatImmeuble', array('asc'=>'cha_surf'));
    foreach ($appart as $k=>$value) {
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble->relate('FrbFlatVille');
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble[0];
    }

    return $appart;

  }

  /**
   *
   * Vérifie le caractère plein des immeuble
   * @return true si les appartements sont plein false sinon
   */
  public function isFull() {
    Doo::loadModelAt("FrbFlatDispo", "frbFlat");
    $full = new FrbFlatDispo();
    $full->dsp_id = 1;
    $full = $this->db()->find($full, array("limit"=>'1'));
    return ($full[0]->dsp_dispo == "0") ? true : false ;
  }

  public function changeDispo() {
    Doo::loadModelAt("FrbFlatDispo", "frbFlat");
    $full = new FrbFlatDispo();
    $full->dsp_id = 1;
    $full = $this->db()->find($full, array("limit"=>'1'));
    $full[0]->dsp_dispo = ($full[0]->dsp_dispo == "0") ? 1 : 0 ;
    $this->db()->update($full[0]);
  }

  public function getOne($id) {
    Doo::loadModelAt('FrbFlatChambre', 'frbFlat');
    Doo::loadModelAt('FrbFlatImmeuble', 'frbFlat');
    Doo::loadModelAt('FrbFlatVille', 'frbFlat');
    $appart = new FrbFlatChambre();
    $appart->cha_id = $id;
    $appart = $appart->relate('FrbFlatImmeuble', array('asc'=>'cha_surf'));
    if (is_null($appart)) {
      return null;
    }
    foreach ($appart as $k=>$value) {
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble->relate('FrbFlatVille');
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble[0];
    }

    return $appart;
  }

  public function getPagination($page = 0, $count = 4, $where = '') {
    Doo::loadModelAt('FrbFlatChambre', 'frbFlat');
    Doo::loadModelAt('FrbFlatImmeuble', 'frbFlat');
    Doo::loadModelAt('FrbFlatVille', 'frbFlat');
    $appart = new FrbFlatChambre();
    if ($where == '')
    $appart = $appart->relate('FrbFlatImmeuble', array('limit'=>''.$page*$count.','.$count, 'asc'=>'cha_surf'));
    else
    $appart = $appart->relate('FrbFlatImmeuble', array('where'=>''.$where, 'limit'=>''.$page*$count.','.$count, 'asc'=>'cha_surf'));
    if (is_null($appart)) {
      return null;
    }
    foreach ($appart as $k=>$value) {
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble->relate('FrbFlatVille');
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble[0];
    }

    return $appart;
  }

  public function getTotalCount() {
    Doo::loadModelAt('FrbFlatChambre', 'frbFlat');
    $appart = new FrbFlatChambre();
    $count = $this->db()->find($appart);
    return sizeof($count);
  }

  /**
   *
   * Renvoie un array avec l'id de l'appartement et la première photo
   * @param integer $number le nombre d'appartements souhaités
   * @return array[](array('id_appart'=>'', 'photo_name'=>''), 'surface'=>'');
   */
  public function getRandom($number = 4) {
    Doo::loadModelAt('FrbFlatChambre', 'frbFlat');
    $apparts = new FrbFlatChambre();
    $apparts = $this->db()->find($apparts, array('limit'=>$number));
    foreach ($apparts as $appart) {
      $picture = $this->getOnePicture($appart->cha_id);
      $picture = (empty($picture)) ? "vide" : $picture[0];
      $arr[] = array('id_appart'=>$appart->cha_id, 'photo_name'=>$picture, 'surface'=>$appart->cha_surf);
    }
    return $arr;
    /*Doo::loadModelAt('FrbFlatChambre', 'frbFlat');
     Doo::loadModelAt('FrbFlatImmeuble', 'frbFlat');
     Doo::loadModelAt('FrbFlatVille', 'frbFlat');
     $appart = new FrbFlatChambre();
     $appart = $appart->relate('FrbFlatImmeuble', array('limit'=> ''.$number));
     foreach ($appart as $k=>$value) {
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble->relate('FrbFlatVille');
      $value->FrbFlatImmeuble = $value->FrbFlatImmeuble[0];
      }

      return $appart;*/
  }

  /**
   *
   * Récupère toutes les images d'un appartement
   * @param $id_flat
   */
  public function getPictures($id_flat) {
    Doo::loadModelAt('FrbFlatPictures', 'frbFlat');
    $pictures = new FrbFlatPictures();
    $pictures->cha_id = $id_flat;
    $pictures = $this->db()->find($pictures, array('asc'=>'pic_date'));
    $arr = array();
    foreach ($pictures as $picture) {
      $arr[] = $picture->cha_pic;
    }
    return $arr;
  }

  public function getOnePicture($id_flat) {
    Doo::loadModelAt('FrbFlatPictures', 'frbFlat');
    $pictures = new FrbFlatPictures();
    $pictures->cha_id = $id_flat;
    $pictures = $this->db()->find($pictures, array('limit'=>'1', 'asc'=>'pic_date'));
    $arr = array();
    foreach ($pictures as $picture) {
      $arr[] = $picture->cha_pic;
    }
    return $arr;
  }

  /**
   *
   * Return true si une réservation est valide. Pas de chevauchements datedeb < datefin, etc.
   * Les paramètres sont sous le format yyyy-mm-dd
   * @param unknown_type $datedeb
   * @param unknown_type $datefin
   */
  public function validReservation($datedeb, $datefin, $id_flat, $chr_id=0) {
    if ($datefin == "") $datefin = "2030-12-31";
    $datedeb = explode('-', $datedeb);
    $datefin = explode('-', $datefin);
    $tmp = new DateTime();
    $datedeb = $tmp->setDate($datedeb[0], $datedeb[1], $datedeb[2]);
    unset($tmp);
    $tmp = new DateTime();
    $datefin = $tmp->setDate($datefin[0], $datefin[1], $datefin[2]);
    unset($tmp);

    if ($datedeb > $datefin) {
      return false;
    }

    Doo::loadModelAt("FrbFlatChambreres", 'frbFlat');
    $reservations = new FrbFlatChambreres();
    $reservations->cha_id = $id_flat;
    $reservations = $this->db()->find($reservations, array('where'=>"chr_id!=$chr_id"));

    foreach($reservations as $reservation) {
      $reservation->chr_datedeb = explode('-', $reservation->chr_datedeb);
      $tmp = new DateTime();
      $reservation->chr_datedeb = $tmp->setDate($reservation->chr_datedeb[0], $reservation->chr_datedeb[1], $reservation->chr_datedeb[2]);

      unset($tmp);

      if ($reservation->chr_datefin=="0000-00-00") $reservation->chr_datefin = "2030-12-31";
      $reservation->chr_datefin = explode('-', $reservation->chr_datefin);
      $tmp = new DateTime();
      $reservation->chr_datefin = $tmp->setDate($reservation->chr_datefin[0], $reservation->chr_datefin[1], $reservation->chr_datefin[2]);
      unset($tmp);

      if ($reservation->chr_datedeb == $datedeb || $reservation->chr_datefin == $datefin
      || ($datedeb < $reservation->chr_datedeb && $datefin > $reservation->chr_datedeb)
      ||  ($datedeb > $reservation->chr_datedeb && $datefin < $reservation->chr_datefin)
      || ($datedeb > $reservation->chr_datedeb && $datedeb < $reservation->chr_datefin) ) {
        return false;
      }
    }

    return true;
  }

  /**
   * @deprecated
   * Enter description here ...
   * @param $appart
   * @param $month
   */
  public function getReservations($appart, $month) {
    Doo::loadControllerAt("Month", "frbFlat");
    Doo::loadModelAt("FrbFlatChambreres", 'frbFlat');
    $reservations = new FrbFlatChambreres();
    $reservations->cha_id = $appart;
    $reservations = $this->db()->find($reservations);
    $reser = null;
    if (empty($reservations)) { return null; }
    foreach ($reservations as $reservation) {
      if ($reservation->chr_datefin == "0000-00-00") {
        $reservation->chr_datefin = "2030-12-31";
      }
      $ae = $this->getDatesBetween($reservation->chr_datedeb, $reservation->chr_datefin);
      foreach ($ae as $a) {
        $tmp_month = explode('-', $a);
        if ($tmp_month[1] == $month) {
          $reser[] = "app$reservation->cha_id-$a";
        }
      }

      //$reser[] = "app$reservation->cha_id-$reservation->chr_datedeb";
    }
    return $reser;
  }

  /**
   *
   * TODO : Fournir un tableau qui contient un tableau avec datedeb et datefin
   * C'est le js qui fera l'opération
   * Les cas sont
   * (datedebut <= fin mois/année en cours && (datefin == null || datefin >= mois/année en cours)
   * @param unknown_type $appart
   * @param unknown_type $month
   * @param unknown_type $year
   */
  public function getSpecReservations($appart, $month, $year) {
    $endMonth = Month::getDaysCount($month, $year);
    $endMonth = $year."-".$month."-".$endMonth;
    $beginMonth = $year."-".$month."-01";
    Doo::loadModelAt("FrbFlatChambreres", 'frbFlat');
    $reservations = new FrbFlatChambreres();
    $reservations = $this->db()->find($reservations,
    array(  "where"=>"cha_id = $appart
          and chr_datedeb <= '".$endMonth."' 
          and (chr_datefin = '0000-00-00' or chr_datefin >='".$beginMonth."') "));
    $reser = null;
    if (empty($reservations)) { return null; }
    foreach ($reservations as $reservation) {
      if ($reservation->chr_datedeb < $beginMonth) {
        $reservation->chr_datedeb = $beginMonth;
      }
      if ($reservation->chr_datefin > $endMonth || $reservation->chr_datefin == "0000-00-00") {
        $reservation->chr_datefin = $endMonth;
      }
      $reser[] = array('datedeb'=> $reservation->chr_datedeb, 'datefin' =>  $reservation->chr_datefin);
    }
    return $reser;
  }


}